package com.shop.shoppingmall.controller;

import com.alibaba.fastjson.JSONObject;
import com.shop.shoppingmall.dao.ShUserDao;
import com.shop.shoppingmall.entity.ShShipAddress;
import com.shop.shoppingmall.entity.ShUser;
import com.shop.shoppingmall.service.ShShipAddressService;
import com.shop.shoppingmall.util.CommonConstant;
import com.shop.shoppingmall.util.JwtUtil;
import com.shop.shoppingmall.util.api.vo.Result;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 收货地址(ShShipAddress)表控制层
 *
 * @author makejava
 * @since 2020-09-09 11:34:54
 */
@RestController
@RequestMapping("shShipAddress")
@CrossOrigin
public class ShShipAddressController {
    /**
     * 服务对象
     */
    @Resource
    private ShShipAddressService shShipAddressService;
    @Resource
    private ShUserDao shUserDao;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ShShipAddress selectOne(String id) {
        return this.shShipAddressService.queryById(id);
    }

    /**
     * 添加收货地址
     * @param shShipAddress
     * @return
     */
    @PostMapping("/addShopAddress")
    public Result addShopAddress(@RequestBody ShShipAddress shShipAddress, HttpServletRequest request){
        String nameByToken = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(nameByToken);
        if(shUser == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        return shShipAddressService.addShopAddress(shShipAddress ,shUser);
    }

    /**
     * 修改收货地址
     * @param shShipAddress
     * @return
     */
    @PostMapping("/updateShopAddress")
    public Result updateShopAddress(@RequestBody ShShipAddress shShipAddress, HttpServletRequest request){
        String nameByToken = JwtUtil.getUserNameByToken(request);
        ShUser shUser = shUserDao.selectLoginName(nameByToken);
        if(shUser == null){
            return Result.error(CommonConstant.SC_OK_201,"您还未注册或者登录，请先注册或登录");
        }
        return shShipAddressService.updateShopAddress(shShipAddress,shUser.getId());
    }

    /**
     * 查询该用户所有的收货地址
     * @param request
     * @return
     */
    @GetMapping("/selectShopAddress")
    public Result selectShopAddress(HttpServletRequest request){
        return shShipAddressService.selectShopAddress(request);
    }

    @PostMapping("/deleteShopAddress")
    public Result deleteShopAddress(@RequestBody JSONObject jsonObject){
        String id = jsonObject.get("id").toString();
        return shShipAddressService.deleteShopAddress(id);
    }


    @GetMapping("/selectByAddress")
    public Result selectByAddress(@Param("id") String id){
        return shShipAddressService.selectByAddress(id);
    }

    /**
     * 查询该用户的默认地址信息
     * @param request
     * @return
     */
    @GetMapping("/selectAddressDeFault")
    public Result selectAddressDeFault(HttpServletRequest request){
        return shShipAddressService.selectAddressDeFault(request);
    }

}